﻿using System;
using System.Data;
using System.Configuration;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace dzs.Web.SysManage
{
    public partial class DistributeRoles : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            DataTable dt = new dzs.BLL.rights().GetAllList().Tables[0];
            int temp = 0;

            DataRow[] drs = dt.Select("parent_id=0");
            for (int i = 0; i < drs.Length; i++)
            {
                Panel panel = new Panel();
                panel.CssClass = "panelRoleStyle";

                CheckBox cb = new CheckBox();
                cb.Text = drs[i][1].ToString();
                cb.ID = "cb" + drs[i][0].ToString();
                //cb.Attributes.Add("onclick", "CheckAll(this.id)");//在父节点checkBox上加入全选事件

                HtmlInputHidden hh = new HtmlInputHidden();
                hh.Value = "0";
                hh.ID = "hh" + drs[i][0].ToString();

                HtmlGenericControl hc = new HtmlGenericControl();
                hc.InnerHtml = "<br />";

                panel.Controls.Add(cb);
                panel.Controls.Add(hh);
                panel.Controls.Add(hc);

                temp = 0;
                LoadRights(dt, temp, panel, Convert.ToInt32(drs[i][0]));

                this.Panel1.Controls.Add(panel);
            }

            DataRow[] drs1 = dt.Select("parent_id=-1");
            for (int j = 0; j < drs1.Length; j++)
            {
                Panel panel = new Panel();
                panel.CssClass = "panelRoleStyle";

                CheckBox cb = new CheckBox();
                cb.Text = drs1[j][1].ToString();
                cb.ID = "cb" + drs1[j][0].ToString();
                //cb.Attributes.Add("onclick", "CheckAll(this.id)");//在父节点checkBox上加入全选事件

                HtmlInputHidden hh = new HtmlInputHidden();
                hh.Value = "0";
                hh.ID = "hh" + drs1[j][0].ToString();

                HtmlGenericControl hc = new HtmlGenericControl();
                hc.InnerHtml = "<br />";


                panel.Controls.Add(cb);
                panel.Controls.Add(hh);
                panel.Controls.Add(hc);

                temp = 0;

                this.Panel1.Controls.Add(panel);
            }

            if (Request.QueryString["roles_id"] != null)
            {
                int roles_id = Convert.ToInt32(Request.QueryString["roles_id"]);

                dzs.Model.roles role = new dzs.BLL.roles().GetModel(roles_id);

                this.lbl_roles.Text = role.roles_name;

                List<int> li = new List<int>();
                DataSet ds = new dzs.BLL.role_right().GetList(" 1=1 and roles_id=" + roles_id);

                foreach (DataRow row in ds.Tables[0].Rows)
                {
                    li.Add(Convert.ToInt32(row["rights_id"].ToString()));
                }
                Checked(li);//选中当前角色的权     
            }
        }
        //提交
        protected void btn_submit_Click(object sender, EventArgs e)
        {
            if (Request.QueryString["roles_id"] != null)
            {
                int roles_id = Convert.ToInt32(Request.QueryString["roles_id"]);

                dzs.BLL.role_right r = new BLL.role_right() ;

                DataSet ds = r.GetList(" 1=1 and roles_id=" + roles_id);

                foreach (DataRow row in ds.Tables[0].Rows)//先删除所有权限
                {
                    r.Delete(Convert.ToInt32(row["role_right_id"].ToString()));
                }

                List<string> li = GetSelectedNode(roles_id);

                foreach (string s in li)
                {
                    string add_roles_id = s.Split(',')[0];
                    string add_rights_id = s.Split(',')[1];

                    dzs.Model.role_right rt = new dzs.Model.role_right();

                    rt.rights_id = Convert.ToInt32(add_rights_id);
                    rt.roles_id = Convert.ToInt32(add_roles_id);

                    r.Add(rt);
                }


                //ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.UpdatePanel1.GetType(), "", "alert('提交成功');location.href='RolesList.aspx'", true);
                Maticsoft.Common.MessageBox.Show(this,"提交成功！");

            }
        }
        //获取选中的所有权限（没有采用递归）
        private List<string> GetSelectedNode(int roleId)
        {
            List<string> li = new List<string>();
            for (int i = 0; i < this.Panel1.Controls.Count; i++)
            {
                if (this.Panel1.Controls[i] is Panel)
                {
                    int len = this.Panel1.Controls[i].Controls.Count;
                    for (int j = 0; j < len; j++)
                    {
                        if (Panel1.Controls[i].Controls[j] is CheckBox)
                        {
                            CheckBox cb = Panel1.Controls[i].Controls[j] as CheckBox;
                            int nodeId = Convert.ToInt32(cb.ID.Substring(2));

                            if (cb.Checked == true)
                            {
                                li.Add(roleId + "," + nodeId);
                            }
                        }
                    }
                }
            }

            return li;
        }

        private void LoadRights(DataTable dt, int temp, Panel panel, int parent_id)
        {
            string text = "";
            DataRow[] drs = dt.Select("parent_id=" + parent_id);

            if (drs.Length != 0)
            {
                temp += 8;
            }

            for (int i = 0; i < temp; i++)
            {
                text += "&nbsp;";
            }

            for (int i = 0; i < drs.Length; i++)
            {
                CheckBox cb = new CheckBox();
                cb.Text = drs[i][1].ToString();
                cb.ID = "cb" + drs[i][0].ToString();


                HtmlInputHidden hh = new HtmlInputHidden();
                hh.Value = parent_id.ToString();
                hh.ID = "hh" + drs[i][0].ToString();

                HtmlGenericControl hc1 = new HtmlGenericControl();
                hc1.InnerHtml = text;
                panel.Controls.Add(hc1);

                panel.Controls.Add(cb);
                panel.Controls.Add(hh);

                //HtmlGenericControl hc = new HtmlGenericControl();
                //hc.InnerHtml = "<br />";
                //panel.Controls.Add(hc);

                LoadRights(dt, temp, panel, Convert.ToInt32(drs[i][0]));
            }
        }

        //选中当前角色已获得的所有权限(没有采用递归)
        private void Checked(List<int> li)
        {
            for (int i = 0; i < Panel1.Controls.Count; i++)
            {
                if (Panel1.Controls[i] is Panel)
                {
                    int len = Panel1.Controls[i].Controls.Count;
                    for (int j = 0; j < len; j++)
                    {
                        if (Panel1.Controls[i].Controls[j] is CheckBox)
                        {
                            CheckBox cb = Panel1.Controls[i].Controls[j] as CheckBox;
                            int nodeId = Convert.ToInt32(cb.ID.Substring(2));

                            if (li.Contains(nodeId))
                            {
                                cb.Checked = true;
                            }
                        }
                    }
                }
            }
        }
    }
}